// Static walls linked list structure
// Used for adding to from map class, and for deleting on map change
#include <iostream>
#include "walls.h"

using namespace std;

Wall_List::Wall_List(void)
{
    first = NULL;
    current = NULL;
}
    

void Wall_List::add_wall(float x, float y, float half_width, float half_height, float temperature, float heat_conductivity)
{
    wall_data  *temp;  //create a temporary node 
    //temp = (wall_data*)malloc(sizeof(wall_data));
    temp = new (wall_data);
    temp->x = x;
    temp->y = y;
    temp->half_width = half_width;
    temp->half_height = half_height;
    temp->state.temperature = temperature;
    temp->state.heat_conductivity = heat_conductivity;

    if (first != NULL)
    {
        // Insert into the first
        temp->next = first;
        first = temp;
    } else {
        first = temp;
    }
}

void Wall_List::start()
{
    current = first;
}

void Wall_List::next()
{
    if (current != NULL)
    {
        current = current->next;
    }
}

// Mainly used for debugging
void Wall_List::print_all()
{
current = first;
    do
    {  
        if (current == NULL)
            cout << "End of list" << endl;
        else {
            print_current();
            current = current->next;
        }
    } while (current != NULL);
}

void Wall_List::print_current()
{
    // Primarily for debugging
    cout << "x,y : " << current->x << ", " << current->y << endl;
    cout << "hwidth, hheight : " << current->half_width << ", " << current->half_height << endl;
    cout << "temperature, conductivity : " << current->state.temperature << ", " << current->state.heat_conductivity << endl;
    cout << endl;
}

void Wall_List::empty_list()
{
    wall_data  *temp;  //create a temporary node
    current = first;
    do
    {  
        if (current == NULL)
            cout << "End of list" << endl;
        else { 
            #ifdef DEBUG
            print_current();
            #endif
            // Move to next node (if present)
            temp = current->next;
            delete current;
            current = temp;
        }
    } while (current != NULL);
    
    // empty the first pointer
    first = NULL;
}
Wall_List::~Wall_List()
{
    empty_list();
}
